home *** CD-ROM | disk | FTP | other *** search
/ Aminet 45 / Aminet 45 (2001)(GTI - Schatztruhe)[!][Oct 2001].iso / Aminet / game / role / ldmud-3.2-bin.lha / mud / mudlib / telnet_neg.txt < prev    next >
Text File  |  2001-06-11  |  7KB  |  184 lines

  1. Hiya, guys!
  2.  
  3. Okay, here is for your attention few bits  of LPC code, that could help  you
  4. to determine: player terminal screen size and player connection lag.
  5.  
  6. With the NAWS option everything is quite clear - your terminal reports it's
  7. size to the driver every time, the driver requests for that (or not, if this
  8. is  some dumb telnet client, like windows' one). In case you change terminal
  9. size  during gameplay, terminal reports it's new size to the driver by itself.
  10. That's it!
  11.  
  12. With the TM (Timing Mark) lag  measuring  accuracy,  everything  is a little
  13. bit more complicated. In  fact  every  player receives the  value,  that is
  14. actually the sum of the two values: the server lag (load), and the telnet
  15. connection lag. 
  16.  
  17. Whether this is suitable for your own mudlib or not, you should decide.
  18.  
  19. While writing these funs, the main target was to get such a  functionality,
  20. that is why the code and even algorythms definitely could be optimized  and
  21. improved.  If you'll get any ideas how to improve this code, do not hesitate
  22. to do that ;)
  23.  
  24. DiEHARD.
  25.  
  26. --------------------------------------------------------------------------------
  27.  
  28. This is a part of the code we use in the master.c file:
  29.  
  30. #include <telnet.h>
  31. /*
  32.             .
  33.             .
  34.             .
  35. */
  36. void inaugurate_master (int arg) {
  37. /*
  38.             .
  39.             .
  40.             .
  41. */
  42.   set_driver_hook(H_TELNET_NEG, "telnet_neg");
  43.   set_driver_hook(H_NOECHO, unbound_lambda( 
  44.     ({ 'flag, 'user }), ({ #',,({ #'?, ({ #'==, 'flag, 1 }), 
  45.       ({ #'binary_message, quote( ({ IAC, WILL, TELOPT_ECHO }) ), 3 }),
  46.       ({ #'binary_message, quote( ({ IAC, WONT, TELOPT_ECHO }) ), 3 })
  47.     }) }) ) );
  48. /*
  49.             .
  50.             .
  51.             .
  52. */
  53.  
  54. This is the part of the living.c code:
  55.  
  56.  
  57. #include <telnet.h>
  58. /*
  59.             .
  60.             .
  61.             .
  62. */
  63. //------------- Functions Prototypes ----------
  64. query_user_lag(void);
  65. int set_naws_flag(void);
  66. int measure_lag();
  67. //---------------------------------------------
  68.  
  69. // ------- Lag Measurement Variables ----------
  70. mixed  start_time;  // Lag measurement variable
  71. mixed  end_time;    // Lag measurement variable
  72. float  lag=-1.0;    // Current player lag
  73. int    snt;         // special flag
  74. // --------------------------------------------
  75.  
  76. // --------------- NAWS Variables -------------
  77. nosave int do_naws_sent, will_naws_rcvd;
  78. // --------------------------------------------
  79.  
  80.  
  81.  
  82. //////////////////////////////////////////////////////
  83. //                                                  //
  84. //       These lines shuold be put somewhere        //
  85. //       where  you'd   like   to  determine        //
  86. //       terminal screen size.                      //
  87. //                                                  //
  88. // binary_message( ({ IAC, DO, TELOPT_NAWS }), 3 ); //
  89. // do_naws_sent=1;                                  //
  90. //                                                  //
  91. //////////////////////////////////////////////////////
  92.  
  93.  
  94. // ----------------------- Lag Measurement Function -----------------------
  95. int measure_lag() {
  96.     if(!snt) {                                       // If Timing Mark was
  97.                                                      // received, send it again
  98.  
  99.       start_time = utime();                          // Mark the time, 
  100.                                                      // we sent Timing Mark
  101.  
  102.       binary_message( ({ IAC, DO, TELOPT_TM }), 3 ); // Send Timing Mark
  103.     }
  104.     snt++;                                           // Increase snt flag by 1
  105.     if(snt>4) {                                      // every time we calling
  106.       lag=-1.00;                                     // measure_lag(). If fun
  107.       snt=0;                                         // was called more than 4
  108.     }                                                // times, and there are
  109.     call_out("measure_lag",2);                       // still no answer - set
  110.                                                      // the value of lag to -1
  111.                                                      // this will return to the
  112.                                                      // requesting user the 
  113.                                                      // string value of "?.??"
  114. }
  115. // ------------------------------------------------------------------------
  116.  
  117. float  query_lag (){ return lag; }
  118. string query_user_lag() { return lag==-1.00?"?.??":sprintf("%.2f",lag); }
  119.  
  120.  
  121. // ------------------ Telnet Negotiations Handler ------------------
  122.  
  123. void telnet_neg( int action, int option, mixed *sb) {
  124.   switch(option) {
  125.                     // --------------------------------
  126.     case TELOPT_TM: // TM (Timing Mark) Handling
  127.                     // According to RFC-860
  128.                     // --------------------------------
  129.             {
  130.             end_time = utime();                      // Mark the time, answer
  131.                                                      // was received
  132.  
  133.             snt=0;                                   // reset snt flag
  134.  
  135.             lag = (end_time[0]-start_time[0]) +      // calculate actual lag
  136.                   (end_time[1]-start_time[1]+500)/1000000.0;
  137.             break;
  138.             }
  139.                      // --------------------------------
  140.    case TELOPT_NAWS: // NAWS (Terminal size) Handling 
  141.                      // According to RFC-1073
  142.                      // --------------------------------
  143.             {
  144.             switch(action) {
  145.  
  146.               case SB: // IAC SB NAWS
  147.                         {
  148.                         do_naws_sent=0;
  149.                         will_naws_rcvd=0;
  150.                         printf("\nYour terminal screen size is %dx%d...\n",
  151.                                 (sb[0]*256)+sb[1],(sb[2]*256)+sb[3]);
  152.                         break;
  153.                         }
  154.                             
  155.               case WILL: // IAC WILL NAWS
  156.                         {
  157.                         will_naws_rcvd=1;
  158.                         if(!do_naws_sent)
  159.                           {
  160.                           binary_message( ({ IAC, DO, TELOPT_NAWS }), 3 );
  161.                           do_naws_sent=1;
  162.                           }
  163.                         break;
  164.                         }
  165.  
  166.               case WONT: // IAC WONT NAWS
  167.                         {
  168.                         do_naws_sent=0;
  169.                         printf("I'm unable to determine your terminal screen size...\n");
  170.                         break;
  171.                         }
  172.  
  173.                default:  // Some unexpected answer received
  174.                         break;
  175.               }
  176.             }
  177.    default: 
  178.             break;
  179.     }
  180.   }
  181. // -----------------------------------------------------------------
  182.  
  183.  
  184.